home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / plaf / basic / BasicComboPopup.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  13.6 KB  |  459 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Dialog;
  8. import java.awt.Dimension;
  9. import java.awt.Point;
  10. import java.awt.Rectangle;
  11. import java.awt.Toolkit;
  12. import java.awt.Window;
  13. import java.awt.event.InputEvent;
  14. import java.awt.event.ItemListener;
  15. import java.awt.event.KeyListener;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseListener;
  18. import java.awt.event.MouseMotionListener;
  19. import java.beans.PropertyChangeListener;
  20. import java.util.EventObject;
  21. import javax.swing.BorderFactory;
  22. import javax.swing.BoxLayout;
  23. import javax.swing.ComboBoxModel;
  24. import javax.swing.JComboBox;
  25. import javax.swing.JComponent;
  26. import javax.swing.JDialog;
  27. import javax.swing.JList;
  28. import javax.swing.JPopupMenu;
  29. import javax.swing.JRootPane;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.ListCellRenderer;
  32. import javax.swing.MenuElement;
  33. import javax.swing.MenuSelectionManager;
  34. import javax.swing.SwingUtilities;
  35. import javax.swing.Timer;
  36. import javax.swing.UIManager;
  37. import javax.swing.border.Border;
  38. import javax.swing.event.ListDataListener;
  39. import javax.swing.event.ListSelectionListener;
  40.  
  41. public class BasicComboPopup extends JPopupMenu implements ComboPopup {
  42.    protected JComboBox comboBox;
  43.    protected JList list;
  44.    protected JScrollPane scroller;
  45.    protected boolean valueIsAdjusting = false;
  46.    protected MouseMotionListener mouseMotionListener;
  47.    protected MouseListener mouseListener;
  48.    protected KeyListener keyListener;
  49.    protected ListSelectionListener listSelectionListener;
  50.    protected ListDataListener listDataListener;
  51.    protected MouseListener listMouseListener;
  52.    protected MouseMotionListener listMouseMotionListener;
  53.    protected PropertyChangeListener propertyChangeListener;
  54.    protected ItemListener itemListener;
  55.    protected Timer autoscrollTimer;
  56.    protected boolean hasEntered = false;
  57.    protected boolean isAutoScrolling = false;
  58.    protected int scrollDirection = 0;
  59.    protected static final int SCROLL_UP = 0;
  60.    protected static final int SCROLL_DOWN = 1;
  61.    private boolean lightNav = false;
  62.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION = "JComboBox.lightweightKeyboardNavigation";
  63.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_ON = "Lightweight";
  64.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_OFF = "Heavyweight";
  65.  
  66.    public BasicComboPopup(JComboBox var1) {
  67.       this.comboBox = var1;
  68.       Object var2 = ((JComponent)var1).getClientProperty("JComboBox.lightweightKeyboardNavigation");
  69.       if (var2 != null) {
  70.          if (var2.equals("Lightweight")) {
  71.             this.lightNav = true;
  72.          } else if (var2.equals("Heavyweight")) {
  73.             this.lightNav = false;
  74.          }
  75.       }
  76.  
  77.       this.mouseListener = this.createMouseListener();
  78.       this.mouseMotionListener = this.createMouseMotionListener();
  79.       this.keyListener = this.createKeyListener();
  80.       this.listSelectionListener = this.createListSelectionListener();
  81.       this.listDataListener = this.createListDataListener();
  82.       this.listMouseListener = this.createListMouseListener();
  83.       this.listMouseMotionListener = this.createListMouseMotionListener();
  84.       this.propertyChangeListener = this.createPropertyChangeListener();
  85.       this.itemListener = this.createItemListener();
  86.       this.list = this.createList();
  87.       this.configureList();
  88.       this.scroller = this.createScroller();
  89.       this.configureScroller();
  90.       this.configurePopup();
  91.       this.installComboBoxListeners();
  92.       this.installKeyboardActions();
  93.    }
  94.  
  95.    // $FF: synthetic method
  96.    static boolean access$0(BasicComboPopup var0) {
  97.       return var0.lightNav;
  98.    }
  99.  
  100.    // $FF: synthetic method
  101.    static void access$1(BasicComboPopup var0, boolean var1) {
  102.       var0.lightNav = var1;
  103.    }
  104.  
  105.    protected void autoScrollDown() {
  106.       int var1 = this.list.getSelectedIndex();
  107.       int var2 = this.list.getModel().getSize() - 1;
  108.       if (var1 < var2) {
  109.          this.valueIsAdjusting = true;
  110.          this.list.setSelectedIndex(var1 + 1);
  111.          this.valueIsAdjusting = false;
  112.          this.list.ensureIndexIsVisible(var1 + 1);
  113.       }
  114.  
  115.    }
  116.  
  117.    protected void autoScrollUp() {
  118.       int var1 = this.list.getSelectedIndex();
  119.       if (var1 > 0) {
  120.          this.valueIsAdjusting = true;
  121.          this.list.setSelectedIndex(var1 - 1);
  122.          this.valueIsAdjusting = false;
  123.          this.list.ensureIndexIsVisible(var1 - 1);
  124.       }
  125.  
  126.    }
  127.  
  128.    protected Rectangle computePopupBounds(int var1, int var2, int var3, int var4) {
  129.       Rectangle var6 = new Rectangle(var1, var2, var3, var4);
  130.       boolean var7 = this.inModalDialog();
  131.       Rectangle var5;
  132.       if (var7) {
  133.          Dialog var8 = this.getDialog();
  134.          if (var8 instanceof JDialog) {
  135.             JRootPane var10 = ((JDialog)var8).getRootPane();
  136.             Point var9 = ((Component)var10).getLocationOnScreen();
  137.             var5 = ((Component)var10).getBounds();
  138.             var5.x = var9.x;
  139.             var5.y = var9.y;
  140.          } else {
  141.             var5 = ((Component)var8).getBounds();
  142.          }
  143.  
  144.          Point var13 = new Point(var5.x, var5.y);
  145.          SwingUtilities.convertPointFromScreen(var13, this.comboBox);
  146.          var5.x = var13.x;
  147.          var5.y = var13.y;
  148.       } else {
  149.          Dimension var14 = Toolkit.getDefaultToolkit().getScreenSize();
  150.          var5 = new Rectangle();
  151.          Point var11 = new Point(0, 0);
  152.          SwingUtilities.convertPointFromScreen(var11, this.comboBox);
  153.          var5.x = var11.x;
  154.          var5.y = var11.y;
  155.          var5.width = var14.width;
  156.          var5.height = var14.height;
  157.       }
  158.  
  159.       if (SwingUtilities.isRectangleContainingRectangle(var5, var6)) {
  160.          return var6;
  161.       } else {
  162.          Rectangle var12 = new Rectangle(0, -var6.height, var6.width, var6.height);
  163.          if (SwingUtilities.isRectangleContainingRectangle(var5, var12)) {
  164.             return var12;
  165.          } else if (var7) {
  166.             SwingUtilities.computeIntersection(var5.x, var5.y, var5.width, var5.height, var6);
  167.             SwingUtilities.computeIntersection(var5.x, var5.y, var5.width, var5.height, var12);
  168.             return var6.height > var12.height ? var6 : var12;
  169.          } else {
  170.             return var12;
  171.          }
  172.       }
  173.    }
  174.  
  175.    protected void configureList() {
  176.       this.list.setFont(this.comboBox.getFont());
  177.       this.list.setForeground(this.comboBox.getForeground());
  178.       this.list.setBackground(this.comboBox.getBackground());
  179.       this.list.setSelectionForeground(UIManager.getColor("ComboBox.selectionForeground"));
  180.       this.list.setSelectionBackground(UIManager.getColor("ComboBox.selectionBackground"));
  181.       this.list.setBorder((Border)null);
  182.       this.list.setCellRenderer(this.comboBox.getRenderer());
  183.       this.list.setRequestFocusEnabled(false);
  184.       this.syncListSelectionWithComboBoxSelection();
  185.       this.list.setSelectionMode(0);
  186.       this.installListListeners();
  187.    }
  188.  
  189.    protected void configurePopup() {
  190.       ((Container)this).setLayout(new BoxLayout(this, 1));
  191.       ((JPopupMenu)this).setBorderPainted(true);
  192.       ((JComponent)this).setBorder(BorderFactory.createLineBorder(Color.black));
  193.       ((JComponent)this).setOpaque(false);
  194.       ((Container)this).add(this.scroller);
  195.       ((JComponent)this).setDoubleBuffered(true);
  196.       ((JComponent)this).setRequestFocusEnabled(false);
  197.    }
  198.  
  199.    protected void configureScroller() {
  200.       this.scroller.setRequestFocusEnabled(false);
  201.       this.scroller.getVerticalScrollBar().setRequestFocusEnabled(false);
  202.       this.scroller.setBorder((Border)null);
  203.    }
  204.  
  205.    protected MouseEvent convertMouseEvent(MouseEvent var1) {
  206.       Point var2 = SwingUtilities.convertPoint((Component)((EventObject)var1).getSource(), var1.getPoint(), this.list);
  207.       MouseEvent var3 = new MouseEvent((Component)((EventObject)var1).getSource(), ((AWTEvent)var1).getID(), ((InputEvent)var1).getWhen(), ((InputEvent)var1).getModifiers(), var2.x, var2.y, ((InputEvent)var1).getModifiers(), var1.isPopupTrigger());
  208.       return var3;
  209.    }
  210.  
  211.    protected ItemListener createItemListener() {
  212.       return new ItemHandler(this);
  213.    }
  214.  
  215.    protected KeyListener createKeyListener() {
  216.       return new InvocationKeyHandler(this);
  217.    }
  218.  
  219.    protected JList createList() {
  220.       return new JList(this.comboBox.getModel());
  221.    }
  222.  
  223.    protected ListDataListener createListDataListener() {
  224.       return new ListDataHandler(this);
  225.    }
  226.  
  227.    protected MouseListener createListMouseListener() {
  228.       return new ListMouseHandler(this);
  229.    }
  230.  
  231.    protected MouseMotionListener createListMouseMotionListener() {
  232.       return new ListMouseMotionHandler(this);
  233.    }
  234.  
  235.    protected ListSelectionListener createListSelectionListener() {
  236.       return new ListSelectionHandler(this);
  237.    }
  238.  
  239.    protected MouseListener createMouseListener() {
  240.       return new InvocationMouseHandler(this);
  241.    }
  242.  
  243.    protected MouseMotionListener createMouseMotionListener() {
  244.       return new InvocationMouseMotionHandler(this);
  245.    }
  246.  
  247.    protected PropertyChangeListener createPropertyChangeListener() {
  248.       return new PropertyChangeHandler(this);
  249.    }
  250.  
  251.    protected JScrollPane createScroller() {
  252.       return new JScrollPane(this.list, 20, 31);
  253.    }
  254.  
  255.    protected void delegateFocus(MouseEvent var1) {
  256.       if (this.comboBox.isEditable()) {
  257.          this.comboBox.getEditor().getEditorComponent().requestFocus();
  258.       } else {
  259.          this.comboBox.requestFocus();
  260.       }
  261.  
  262.    }
  263.  
  264.    private Dialog getDialog() {
  265.       Container var1;
  266.       for(var1 = this.comboBox.getParent(); var1 != null && !(var1 instanceof Dialog) && !(var1 instanceof Window); var1 = ((Component)var1).getParent()) {
  267.       }
  268.  
  269.       return var1 instanceof Dialog ? (Dialog)var1 : null;
  270.    }
  271.  
  272.    public KeyListener getKeyListener() {
  273.       return this.keyListener;
  274.    }
  275.  
  276.    public JList getList() {
  277.       return this.list;
  278.    }
  279.  
  280.    public MouseListener getMouseListener() {
  281.       return this.mouseListener;
  282.    }
  283.  
  284.    public MouseMotionListener getMouseMotionListener() {
  285.       return this.mouseMotionListener;
  286.    }
  287.  
  288.    protected int getPopupHeightForRowCount(int var1) {
  289.       int var2 = this.comboBox.getModel().getSize();
  290.       int var3 = Math.min(var1, var2);
  291.       int var4 = 0;
  292.       ListCellRenderer var5 = this.list.getCellRenderer();
  293.       Object var6 = null;
  294.  
  295.       for(int var7 = 0; var7 < var3; ++var7) {
  296.          var6 = this.list.getModel().getElementAt(var7);
  297.          Component var8 = var5.getListCellRendererComponent(this.list, var6, var7, false, false);
  298.          var4 += var8.getPreferredSize().height;
  299.       }
  300.  
  301.       return var4 == 0 ? 100 : var4;
  302.    }
  303.  
  304.    public void hide() {
  305.       MenuSelectionManager var1 = MenuSelectionManager.defaultManager();
  306.       MenuElement[] var2 = var1.getSelectedPath();
  307.  
  308.       for(int var3 = 0; var3 < var2.length; ++var3) {
  309.          if (var2[var3] == this) {
  310.             var1.clearSelectedPath();
  311.             break;
  312.          }
  313.       }
  314.  
  315.       this.comboBox.repaint();
  316.    }
  317.  
  318.    private boolean inModalDialog() {
  319.       return this.getDialog() != null;
  320.    }
  321.  
  322.    protected void installComboBoxListeners() {
  323.       this.comboBox.addPropertyChangeListener(this.propertyChangeListener);
  324.       this.comboBox.addItemListener(this.itemListener);
  325.       this.installComboBoxModelListeners(this.comboBox.getModel());
  326.    }
  327.  
  328.    protected void installComboBoxModelListeners(ComboBoxModel var1) {
  329.       if (var1 != null) {
  330.          var1.addListDataListener(this.listDataListener);
  331.       }
  332.  
  333.    }
  334.  
  335.    protected void installKeyboardActions() {
  336.    }
  337.  
  338.    protected void installListListeners() {
  339.       this.list.addListSelectionListener(this.listSelectionListener);
  340.       this.list.addMouseMotionListener(this.listMouseMotionListener);
  341.       this.list.addMouseListener(this.listMouseListener);
  342.    }
  343.  
  344.    public boolean isFocusTraversable() {
  345.       return false;
  346.    }
  347.  
  348.    public void show() {
  349.       Dimension var1 = this.comboBox.getSize();
  350.       var1.setSize(var1.width, this.getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
  351.       Rectangle var2 = this.computePopupBounds(0, this.comboBox.getBounds().height, var1.width, var1.height);
  352.       this.scroller.setMaximumSize(var2.getSize());
  353.       this.scroller.setPreferredSize(var2.getSize());
  354.       this.scroller.setMinimumSize(var2.getSize());
  355.       this.list.invalidate();
  356.       this.syncListSelectionWithComboBoxSelection();
  357.       this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
  358.       ((JPopupMenu)this).setLightWeightPopupEnabled(this.comboBox.isLightWeightPopupEnabled());
  359.       ((JPopupMenu)this).show(this.comboBox, var2.x, var2.y);
  360.    }
  361.  
  362.    protected void startAutoScrolling(int var1) {
  363.       if (this.isAutoScrolling) {
  364.          this.autoscrollTimer.stop();
  365.       }
  366.  
  367.       this.isAutoScrolling = true;
  368.       if (var1 == 0) {
  369.          this.scrollDirection = 0;
  370.          Point var2 = SwingUtilities.convertPoint(this.scroller, new Point(1, 1), this.list);
  371.          int var3 = this.list.locationToIndex(var2);
  372.          this.valueIsAdjusting = true;
  373.          this.list.setSelectedIndex(var3);
  374.          this.valueIsAdjusting = false;
  375.          1 var4 = new 1(this);
  376.          this.autoscrollTimer = new Timer(100, var4);
  377.       } else if (var1 == 1) {
  378.          this.scrollDirection = 1;
  379.          Dimension var6 = this.scroller.getSize();
  380.          Point var7 = SwingUtilities.convertPoint(this.scroller, new Point(1, var6.height - 1 - 2), this.list);
  381.          int var8 = this.list.locationToIndex(var7);
  382.          this.valueIsAdjusting = true;
  383.          this.list.setSelectedIndex(var8);
  384.          this.valueIsAdjusting = false;
  385.          2 var5 = new 2(this);
  386.          this.autoscrollTimer = new Timer(100, var5);
  387.       }
  388.  
  389.       this.autoscrollTimer.start();
  390.    }
  391.  
  392.    protected void stopAutoScrolling() {
  393.       this.isAutoScrolling = false;
  394.       if (this.autoscrollTimer != null) {
  395.          this.autoscrollTimer.stop();
  396.          this.autoscrollTimer = null;
  397.       }
  398.  
  399.    }
  400.  
  401.    void syncListSelectionWithComboBoxSelection() {
  402.       int var1 = this.comboBox.getSelectedIndex();
  403.       if (var1 == -1) {
  404.          this.list.clearSelection();
  405.       } else {
  406.          this.list.setSelectedIndex(var1);
  407.       }
  408.  
  409.    }
  410.  
  411.    protected void togglePopup() {
  412.       if (((JPopupMenu)this).isVisible()) {
  413.          this.hide();
  414.       } else {
  415.          this.show();
  416.       }
  417.  
  418.    }
  419.  
  420.    protected void uninstallComboBoxModelListeners(ComboBoxModel var1) {
  421.       if (var1 != null) {
  422.          var1.removeListDataListener(this.listDataListener);
  423.       }
  424.  
  425.    }
  426.  
  427.    protected void uninstallKeyboardActions() {
  428.    }
  429.  
  430.    public void uninstallingUI() {
  431.       this.comboBox.removePropertyChangeListener(this.propertyChangeListener);
  432.       this.comboBox.removeItemListener(this.itemListener);
  433.       this.uninstallComboBoxModelListeners(this.comboBox.getModel());
  434.       this.uninstallKeyboardActions();
  435.    }
  436.  
  437.    protected void updateListBoxSelectionForEvent(MouseEvent var1, boolean var2) {
  438.       Point var3 = var1.getPoint();
  439.       if (this.list != null) {
  440.          int var4 = this.list.locationToIndex(var3);
  441.          if (var4 == -1) {
  442.             if (var3.y < 0) {
  443.                var4 = 0;
  444.             } else {
  445.                var4 = this.comboBox.getModel().getSize() - 1;
  446.             }
  447.          }
  448.  
  449.          if (this.list.getSelectedIndex() != var4) {
  450.             this.list.setSelectedIndex(var4);
  451.             if (var2) {
  452.                this.list.ensureIndexIsVisible(var4);
  453.             }
  454.          }
  455.  
  456.       }
  457.    }
  458. }
  459.